import numpy as np
class speed_get():
    def __init__(self,Dmin,Dmax,H,B0,image,speed_limit):
        self.Dmin=Dmin
        self.Dmax=Dmax
        self.H=H
        self.B0=B0
        self.w = image.shape[0]
        self.h = image.shape[1]
        self.speed_limit=speed_limit
 
    def get_realpoint(self,point):
 
        a_angle=np.arctan(self.Dmin/self.H)
        b_angle=np.arctan(self.Dmax/self.H)-a_angle
        plusb_angle=((self.h-point[1])/self.h)*b_angle
        y1=self.H*np.tan(a_angle+plusb_angle)
        B1=(y1+self.Dmin)*(self.B0/self.Dmax)
        x1=(2*B1*(point[0]-(self.w/2)))/self.w
        real_point=np.array([x1,y1])
        return real_point
 
    def get_speed(self,point1,point2,interval):
        real_point1=self.get_realpoint(point1)
        real_point2=self.get_realpoint(point2)
        distant=np.linalg.norm(real_point1-real_point2)
        speed=distant/interval
        speed=speed*3.6
 
        return speed